package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.account.Account;
import com.acme.bankapp.domain.account.CheckingAccount;
import com.acme.bankapp.domain.account.SavingAccount;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.client.Client;
import com.acme.bankapp.domain.client.Gender;
import com.acme.bankapp.domain.exceptions.ClientExistsException;

public class BankService {
	
	private static int accountsNumber;
	
	public void addClient(Bank bank, Client client) throws ClientExistsException {
		for (Client oldClient : bank.getClients()) {
			if (client.getName().equals(oldClient.getName())) {
				throw new ClientExistsException("Клиент с именем " + client.getName() +
									" уже существует");
			}
		}
		bank.addClient(client);
	}
	
	public SavingAccount createSavingAccount(double amount) {
		return new SavingAccount(getAccountNextId(), amount);
	}
	
	public Client createClient(String name, Gender gender) {
		return new Client(name, gender);
	}
	
	public CheckingAccount createCheckingAccount(double amount, double overdraft) {
		return new CheckingAccount(getAccountNextId(), amount, overdraft);
	}
	
	public void addAccount(Client client, Account account) {
		client.setAccount(account);
	}
	
	public Bank createBank() {
		return new Bank();
	}
	
	public void printMaximumAmountToWithdraw(Bank bank) {
		for (Client client : bank.getClients()) {
			if (client.getAccount() != null) {
				System.out.println("Maximum amount to withdraw for " + client.getName());
				System.out.println(client.getAccount().maximumAmountToWithdraw());				
			}
		}
	}
	
	private static int getAccountNextId() {
		return accountsNumber++;
	}
}
